package com.study._1_linked_lister;

public class LinkedUtils {

    public static void print(LinkedNode rootNode) {
        if (rootNode == null) {
            return;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(rootNode.data);
        while (rootNode.next != null) {
            rootNode = rootNode.next;
            sb.append(", ");
            sb.append(rootNode.data);
        }
        System.out.println(sb.toString());
    }

    public static LinkedNode build(int... dataList) {
        if (dataList == null || dataList.length <= 0) {
            return null;
        }

        LinkedNode node = new LinkedNode(dataList[dataList.length - 1]);

        for (int i = dataList.length - 2; i >= 0; i--) {
            LinkedNode temp = new LinkedNode(dataList[i]);
            temp.next = node;
            node = temp;
        }
        return node;
    }

    public static LinkedNode findLastNode(LinkedNode rootNode) {
        if (rootNode == null) {
            return null;
        }
        while (rootNode.next != null) {
            rootNode = rootNode.next;
        }
        return rootNode;
    }
}
